package com.jz.beijianbao.beijian.controller;


import com.jz.beijianbao.beijian.bean.BeiJianAdd;
import com.jz.beijianbao.beijian.bean.BeiJianSelectVo;
import com.jz.beijianbao.beijian.bean.BeiJianUpdate;
import com.jz.beijianbao.beijian.bean.BeiJianUpdatestatus;
import com.jz.beijianbao.beijian.service.BeiJianService;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@RestController
@CrossOrigin
public class BeiJianTest {

    @Autowired
    private BeiJianService beiJianService;

    //状态下拉框
    //subclass包中已有，直接调用

    //备件小类下拉框
    @GetMapping("subClass/queryAll")
    public ResultVO subClassQuery(){
        ResultVO resultVO = beiJianService.subClassQuery();
        return resultVO;
    }

    //计划大类下拉框
    @GetMapping("planClass/queryAll")
    public ResultVO planClassQueryAll(){
        ResultVO resultVO = beiJianService.planClassQueryAll();
        return resultVO;
    }
    //法人多选框
    @GetMapping("legalPerson/ofCurrent")
    public ResultVO legalPerson(){
        ResultVO resultVO = beiJianService.legalPerson();
        return resultVO;
    }

    //分页
    @PostMapping("spare/queryPage")
    public ResultVO querypage(@RequestBody BeiJianSelectVo beiJianSelectVo){
        if(beiJianSelectVo.getPageIndex()==null){
            beiJianSelectVo.setPageIndex(1);
        }
        if(beiJianSelectVo.getPageSize()==null){
            beiJianSelectVo.setPageSize(10);
        }
        ResultVO resultVO = beiJianService.queryPage(beiJianSelectVo);
        return resultVO;
    }

    //新增
    @PostMapping("spare/add")
    public ResultVO add(@RequestBody BeiJianAdd beiJianAdd){
        ResultVO resultVO = beiJianService.add(beiJianAdd);
        return resultVO;
    }

    //修改
    @PostMapping("spare/update")
    public ResultVO update(@RequestBody @Valid BeiJianUpdate beiJianUpdate, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return beiJianService.updatePlant(beiJianUpdate);
    }

    //启用禁用
    @PostMapping("spare/updatestatus")
    public ResultVO updateStatus(@RequestBody @Valid BeiJianUpdatestatus beiJianUpdatestatus, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return beiJianService.updateStatus(beiJianUpdatestatus);
    }
}